# rofi -show 自定义 -modi "自定义:~/rofi.sh"
#   1: 上述命令可调用rofi.sh作为自定义脚本
#   2: 将打印的内容作为rofi的选项
#   3: 每次选中后 会用选中项作为入参再次调用脚本
#   4: 当没有输出时 整个过程结束

#source ~/.profile

# !!! 确保所有item没有重复的 !!!   שּׂ ﯎ ﴴ 
main_menu_item[1]=" set wallpaper"       ; main_cmd[1]='~/.dwm/scripts/set-wallpaper.sh; main_menu'
# main_menu_item[2]="墳 open_pavucontrol"     ; main_cmd[2]='~/.dwm/scripts/app-starter.sh open_pavucontrol'
main_menu_item[2]="墳 open_pavucontrol"     ; main_cmd[2]='pavucontrol >> /dev/null 2>&1 & '
main_menu_item[3]=" bluetooth"            ; main_cmd[3]='bluetooth_menu'
main_menu_item[4]=" open last sreenshot"  ; main_cmd[4]='~/.dwm/scripts/app-starter.sh open_last_screenshot'
main_menu_item[5]=" toggle some server"   ; main_cmd[5]='server_menu'
main_menu_item[6]=" power_menu"           ; main_cmd[6]='power_menu'
# main_menu_item[7]="Lock blurlock"        ; main_cmd[7]='~/.dwm/scripts/app-starter.sh blurlock >> /dev/null 2>&1 &'

power_menu_item[1]=" （锁屏）Lock"       ; power_cmd[1]='~/.dwm/scripts/i3lock-get-screen-image-fancy.sh >> /dev/null 2>&1 &'
# power_menu_item[1]=" （锁屏）Lock"       ; power_cmd[1]='chesen_lock'
power_menu_item[2]=" （注销）Logout "      ; power_cmd[2]='chosen_logout'
power_menu_item[3]=" （重启）Restart "   ; power_cmd[3]='chosen_reboot'
power_menu_item[4]=" （关机）Shutdown"   ; power_cmd[4]='chosen_shutdown'


# if [ $GO111MODULE = 'on' ]; then
#     main_menu_item[5]="close GO111MODULE"   ; main_cmd[5]='sed -i "s/GO111MODULE=.*/GO111MODULE=off/g" ~/.profile'
# else
#     main_menu_item[5]="open GO111MODULE"   ; main_cmd[5]='sed -i "s/GO111MODULE=.*/GO111MODULE=on/g" ~/.profile'
# fi

server_menu_item[1]="v2raya"             ; server_cmd[1]='[ "$(docker ps | grep v2raya)" ] && docker stop v2raya >> /dev/null || docker restart v2raya >> /dev/null;'
server_menu_item[2]="picom"              ; server_cmd[2]='killall picom || ~/.dwm/scripts/app-starter.sh picom >> /dev/null 2>&1 &'
server_menu_item[3]="easyeffects"        ; server_cmd[3]='killall easyeffects || ~/.dwm/scripts/app-starter.sh easyeffects'
server_menu_item[5]="aria2c"             ; server_cmd[5]='killall aria2c || ~/.dwm/scripts/app-starter.sh aria2c'
# server_menu_item[6]="virsual arch"       ; server_cmd[6]='[ "$(docker ps | grep minearch)" ] && docker stop minearch >> /dev/null || docker restart minearch >> /dev/null && docker exec -u root -itd arch bash -c "/usr/sbin/vncserver :1" ;'

bluetooth_menu_item[1]="toggle A2DP/HSP/HFP" ; bluetooth_cmd[1]='~/.dwm/scripts/app-starter.sh toggle_hp_sink'
bluetooth_menu_item[2]="connect hp1"     ; bluetooth_cmd[2]='~/.dwm/scripts/bluetooth.sh hp >> /dev/null &'
bluetooth_menu_item[3]="connect hp2"     ; bluetooth_cmd[3]='~/.dwm/scripts/bluetooth.sh hp2 >> /dev/null &'
bluetooth_menu_item[4]="disconnect hp1"  ; bluetooth_cmd[4]='~/.dwm/scripts/bluetooth.sh dhp >> /dev/null &'
bluetooth_menu_item[5]="disconnect hp2"  ; bluetooth_cmd[5]='~/.dwm/scripts/bluetooth.sh dhp2 >> /dev/null &'

main_menu() {
    echo -e "\0prompt\x1fmenu"
    for item in "${main_menu_item[@]}"; do
        echo "$item"
    done
}
power_menu() {
    #echo -e "\0prompt\x1fserver"
    echo -e "\0prompt\x1f"
    for item in "${power_menu_item[@]}"; do
        echo "$item"
    done
}
server_menu() {
    echo -e "\0prompt\x1fserver"
    for item in "${server_menu_item[@]}"; do
        echo "$item"
    done
}
bluetooth_menu() {
    echo -e "\0prompt\x1fbluetooth"
    for item in "${bluetooth_menu_item[@]}"; do
        echo "$item"
    done
}



# 
chosen_shutdown(){
    systemctl poweroff
}
chosen_reboot(){
    systemctl reboot
}
chesen_lock(){
    
    if [[ -f /usr/bin/i3lock ]]; then
        if [[ -f ~/.dwm/scripts/app-starter.sh ]];then
            /bin/bash ~/.dwm/scripts/app-starter.sh blurlock
        elif [[ -f ~/.dwm/scripts/i3lock-get-screen-image-fancy.sh ]];then
           ~/.dwm/scripts/i3lock-get-screen-image-fancy.sh
        else
            i3lock
        fi
    elif [[ -f /usr/bin/slock ]]; then
        # /bin/bash ~/.config/i3/scripts/xslock.sh
        /bin/bash ~/.dwm/scripts/scripts/xslock.sh
    else
        notifystr="没有找到相关锁屏软件（i3lock、slock）位置。"
        notify-send $notifystr;
    fi
}

chosen_logout(){
    if [[ "$DESKTOP_SESSION" == "Openbox" ]]; then
        openbox --exit
    elif [[ "$DESKTOP_SESSION" == "bspwm" ]]; then
        bspc quit
    elif [[ "$DESKTOP_SESSION" == "i3" ]]; then
        i3-msg exit
    elif [[ "$DESKTOP_SESSION" == "dwm" || "$DESKTOP_SESSION" == "DWM" ]]; then
        # 需要dwm 打fsignal补丁
        # xsetroot -name "fsignal:<signum>"
        xsetroot -name "fsignal:1"
    else
        notifystr="不知道该窗口退出命令。$DESKTOP_SESSION"
        notify-send $notifystr;
    fi
}


[ ! "$*" ] && main_menu
for i in "${!main_menu_item[@]}"; do
    [ "$*" = "${main_menu_item[$i]}" ] && eval "${main_cmd[$i]}"
done
for i in "${!server_menu_item[@]}"; do
    [ "$*" = "${server_menu_item[$i]}" ] && eval "${server_cmd[$i]}"
done
for i in "${!bluetooth_menu_item[@]}"; do
    [ "$*" = "${bluetooth_menu_item[$i]}" ] && eval "${bluetooth_cmd[$i]}"
done
for i in "${!power_menu_item[@]}"; do
    [ "$*" = "${power_menu_item[$i]}" ] && eval "${power_cmd[$i]}"
done